package com.shuziren.controller.QuestionController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuziren.core.vo.Result;
import com.shuziren.question.entity.dto.InterviewCategoryDTO;
import com.shuziren.question.entity.QuestionCategory;

import com.shuziren.question.service.QuestionCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理题目分类相关的HTTP请求
 * @author 123
 */
@RestController
@RequestMapping("/api/category")
public class QuestionCategoryController {
    
    @Autowired
    private QuestionCategoryService categoryService;


    /**
     * 分类和题目的两表联查，展示分类的所有数据并统计每个分类下的题目数量
     * @return 返回查询结果列表
     */
    @PostMapping("/categoryWithQuestionCount")
    /**
     * 查询
     */
    public Result<IPage<InterviewCategoryDTO>> getCategoryWithQuestionCount(@RequestBody QuestionCategory category) {

            IPage<InterviewCategoryDTO> interviewCategoryDTOIPage = categoryService.selectCategoryWithQuestionCount(category);
            if (interviewCategoryDTOIPage.getRecords() != null){
                return Result.success(interviewCategoryDTOIPage);
            }
            return null;
    }


    /**
     * 添加题目分类
     * @param category
     * @return 返回添加结果
     */
    @PostMapping("/add")
    public Result<Boolean> addCategory(@RequestBody QuestionCategory category) {
        return Result.success(categoryService.save(category));
    }


    /**
     * 更新题目分类
     * @param id
     * @param category
     * @return 返回更新结果
     */
    @PutMapping("/update/{id}")
    public Result<Boolean> updateCategory(@PathVariable("id") Integer id, @RequestBody QuestionCategory category) {
        category.setId(id);
        return Result.success(categoryService.updateById(category));
    }

    /**
     * 删除题目分类
     * @param id
     * @return 返回删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteCategory(@PathVariable("id") Integer id) {
        categoryService.removeById(id);
        return Result.success("删除成功");
    }
} 